package com.guigu.www.denghao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guigu.www.denghao.service.DHCountMemberService;
import com.guigu.www.denghao.service.DHMemberService;
import com.guigu.www.mapper.*;
import com.guigu.www.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DHCountMemberServiceImpl implements DHCountMemberService {

    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private BespeakMapper bespeakMapper;
    @Autowired
    private HealthManagerMapper healthManagerMapper;
    @Autowired
    private SingleDayAppointmentMapper appointmentMapper;
    @Autowired
    private BespeakSetMealMapper setMealMapper;
    @Autowired
    private InterventionMapper interventionMapper;
    @Autowired
    private ReturnDiaryMapper returnDiaryMapper;

    @Override
    public Integer[] queryMemberCount(String[] dates) {
        Integer[] strings = new Integer[dates.length];
        for (int i = 0; i < dates.length; i++) {
            Integer count = memberMapper.selectCount(new QueryWrapper<Member>().like("mem_create_date", dates[i]));
            strings[i] = count;
        }
        return strings;
    }

    @Override
    public Integer[] queryHealthCount(String[] dates) {
        Integer[] integers = new Integer[dates.length];
        for (int i = 0; i < dates.length; i++) {
            Integer count = bespeakMapper.selectCount(new QueryWrapper<Bespeak>().like("bes_time", dates[i]).eq("bes_state",1));
            integers[i] = count;
        }
        return integers;
    }

    @Override
    public Map<String, String> queryTotalAndAdd() {
        // 获取当前时间
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String time = dateFormat.format( now );
        Integer addMember = memberMapper.selectCount(new QueryWrapper<Member>().like("mem_create_date", time));
        Integer addHealth = bespeakMapper.selectCount(new QueryWrapper<Bespeak>().like("bes_time", time).eq("bes_state",1));
        Integer totalMember = memberMapper.selectCount(null);
        Integer totalHealth = bespeakMapper.selectCount(new QueryWrapper<Bespeak>().eq("bes_state",1));
        Map<String,String> map = new HashMap<>();
        map.put("addMember",addMember+"");
        map.put("addHealth",addHealth+"");
        map.put("totalMember",totalMember+"");
        map.put("totalHealth",totalHealth+"");
        //System.out.println(map);
        return map;
    }

    @Override
    public Map<String, String> indexDetails() {
        // 获取当前时间
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String time = dateFormat.format( now );
        Integer healthNumber = healthManagerMapper.selectCount(new QueryWrapper<HealthManager>().eq("role_id", 2));
        Integer settingPersonNum = appointmentMapper.queryCanapp(time);
        Integer historySale = healthManagerMapper.queryTotalMoney();
        Integer daySale = healthManagerMapper.queryDayMoney("%"+time+"%");
        Map<String,String> map = new HashMap<>();
        map.put("healthNumber",healthNumber+"");
        map.put("settingPersonNum",settingPersonNum+"");
        map.put("historySale",historySale+"");
        map.put("daySale",daySale+"");
        return map;
    }

    @Override
    public Integer[] querySaleMoney(String[] dates) {
        Integer[] integers = new Integer[dates.length];
        for (int i = 0; i < dates.length; i++) {
            Integer count = healthManagerMapper.queryDayMoney("%"+dates[i]+"%");
            if (count == null){
                count = 0;
            }
            integers[i] = count;
        }
        return integers;
    }

    @Override
    public Map<String, String> querySetMealSale() {
        List<BespeakSetMeal> setMeals = setMealMapper.selectList(new QueryWrapper<BespeakSetMeal>().eq("bes_sm_state", 0));
        Map<String,String> map = new HashMap<>();
        for (BespeakSetMeal setMeal : setMeals) {
            Integer count = bespeakMapper.selectCount(new QueryWrapper<Bespeak>().eq("bes_sm_id", setMeal.getBesSmId()).eq("bes_state",1));
            map.put(setMeal.getBesSmName(),count+"");
        }
        return map;
    }

    @Override
    public Map<String,Integer[]> queryIntervention(String[] dates, Integer hmId) {
        Integer[] integers = new Integer[dates.length];
        Integer[] integers1 = new Integer[dates.length];
        for (int i = 0; i < dates.length; i++) {
            Integer count = interventionMapper.selectCount(new QueryWrapper<Intervention>().like("inter_plan_create_time",dates[i]));
            Integer count1 = interventionMapper.selectCount(new QueryWrapper<Intervention>().like("inter_plan_create_time", dates[i]).eq("hm_id", hmId));
            if (count == null){
                count = 0;
            }
            if (count1 == null){
                count1 = 0;
            }
            integers[i] = count;
            integers1[i] = count1;
        }
        Map<String,Integer[]> map = new HashMap<>();
        map.put("schemeTotal",integers);
        map.put("personTotal",integers1);
        return map;
    }

    @Override
    public Map<String, Integer[]> queryReturnCount(String[] dates, Integer hmId) {
        Integer[] integers = new Integer[dates.length];
        Integer[] integers1 = new Integer[dates.length];
        for (int i = 0; i < dates.length; i++) {
            Integer count = returnDiaryMapper.selectCount(new QueryWrapper<ReturnDiary>().like("re_diary_time",dates[i]));
            Integer count1 = returnDiaryMapper.selectCount(new QueryWrapper<ReturnDiary>().like("re_diary_time", dates[i]).eq("hm_id", hmId));
            if (count == null){
                count = 0;
            }
            if (count1 == null){
                count1 = 0;
            }
            integers[i] = count;
            integers1[i] = count1;
        }
        Integer count = returnDiaryMapper.selectCount(null);
        Integer[] diaryTotalCount = new Integer[1];
        diaryTotalCount[0] = count;
        Map<String,Integer[]> map = new HashMap<>();
        map.put("diaryTotal",integers);
        map.put("salfTotal",integers1);
        map.put("diaryTotalCount",diaryTotalCount);
        return map;
    }

    @Override
    public Map<String, Integer> queryInterventionInfo(Integer hmId, String jin, String zuo) {
        Integer count = interventionMapper.selectCount(null);
        Integer zuoSalf = returnDiaryMapper.selectCount(new QueryWrapper<ReturnDiary>().like("re_diary_time", zuo).eq("hm_id", hmId));
        Integer count1 = returnDiaryMapper.selectCount(new QueryWrapper<ReturnDiary>().like("re_diary_time", zuo));
        Integer count2 = returnDiaryMapper.selectCount(new QueryWrapper<ReturnDiary>().like("re_diary_time", jin));
        Map<String,Integer> map = new HashMap<>();
        map.put("zongCount",count);
        map.put("zuoSalfCount",zuoSalf);
        map.put("zuoCount",count1);
        map.put("jinCount",count2);
        return map;
    }
}
